package com.fp.vavr.example;

import io.vavr.Function1;
import io.vavr.Function2;
import io.vavr.Tuple;
import io.vavr.Tuple2;
import io.vavr.collection.List;
import io.vavr.control.Option;
import org.junit.Test;

/**
 * Created by user on 17/9/11.
 */
public class CollectTest {
    @Test
    public void list(){
        List<Integer> list1 = List.of(1, 2, 3);
        for(int a : list1){
            System.out.println(a);
        }

    }

    @Test
    public void lift(){
        Function2<Integer, Integer, Option<Integer>> sum = Function2.lift(this::sum);
        Option<Integer> optionalResult = sum.apply(-1, 2);
        System.out.println(optionalResult.get());
    }

    private int sum(int first, int second) {
        if (first < 0 || second < 0) {
            throw new IllegalArgumentException("Only positive integers are allowed");
        }
        return first + second;
    }

    @Test
    public void tuple(){
        Tuple2<String,Integer> java8 = Tuple.of("Java", 8);
        System.out.println(java8._1);
    }

    @Test
    public void composition(){
        Function1<Integer, Integer> plusOne = a -> a + 1;
        Function1<Integer, Integer> multiplyByTwo = a -> a * 2;
        Function1<Integer, Integer> add1AndMultiplyBy2 = plusOne.andThen(multiplyByTwo);
        Integer result = add1AndMultiplyBy2.apply(2);
    }

}
